Java Projects by Peter Verhas

Java Projects by Peter Verhas

Author:Peter Verhas
Language: eng
Format: epub
Publisher: Packt
Published: 2018-08-31T06:32:02+00:00


BlockingQueue

BlockingQueue is an interface that extends the standard Queue interface with methods that are suitable for use by multithread applications. Any implementation of this interface provides methods that allow different threads to put an element into the queue, pull elements off the queue, and wait for elements that are in the queue.

When there is a new element to be stored in the queue, you can add() it, offer() it, or put() it. These are the names of the methods that store elements and they do the same thing, just a bit differently. The add() method throws an exception if the queue is full and there is no room for the element. The offer() method does not throw an exception but returns either true or false, depending on whether the operation is successful. If it can store the element in the queue, it returns true. There is also a version of offer() that specifies a timeout. That version of the method waits and returns only false if it cannot store the value in the queue during the period. The put() method is the simplest version; it waits until it can do its job.

When talking about the available room in a queue, do not get puzzled and confuse it with general Java memory management. If there is no more memory, and the garbage collector is also unable to release any, you will certainly get an OutOfMemoryError. An exception is thrown by add(), and the false value is returned by offer() when the queue limits are reached. Some of the BlockingQueue implementations can limit the number of elements that can be stored in a queue simultaneously. If that limit is reached, then the queue is full and cannot accept more elements.

There are four different ways of fetching elements from a BlockingQueue implementation. In this direction, the special case is when the queue is empty. In that case, the remove() method throws an exception instead of returning the element, the poll() method returns null if there is no element, and the take() method just waits until it can return an element.

Finally, there are two methods inherited from the Queues interface that do not consume the element from the queue, but just look at it. The element() method returns the head of the queue or throws an exception if the queue is empty. The peek() method returns null if there is no element in the queue. The following table summarizes the operations borrowed from the documentation of the interface:

Throws exception Special value Blocks Times out

Insert add(e) offer(e) put(e) offer(e, time, unit)

Remove remove() poll() take() poll(time, unit)

Examine element() peek() not applicable not applicable



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.